from bank.Bank import Bank
from bank.User import User

if __name__ == '__main__':
    bank = Bank({}, "中国工商银行",5)
    while True:
        print("**********************")
        print("*     中国工商银行     *")
        print("*     账户管理系统     *")
        print("*        V1.0        *")
        print("**********************")
        print("*                    *")
        print("*     1.开户          *")
        print("*     2.存钱          *")
        print("*     3.取钱          *")
        print("*     4.转账          *")
        print("*     5.查询          *")
        print("*     6.退出          *")
        print("*                    *")
        print("**********************")
        operation = input("请选择操作功能: \t")

        if operation == "1":
            user = User.add_user()
            # 判断用户是否属于本银行
            if user.bank == bank.bank_name:
                res = bank.add_user(user)
                # 提示消息
                print("用户{}开户成功,账户为{}".format(user.name, user.account)) if res == 1 else print(
                    "用户{}开户失败,原因: 用户名重复".format(user.name)) if res == 2 else print(
                    "用户{}开户失败,原因: 用户数量已满".format(user.name))
            else:
                print("用户{}不是{}银行用户".format(user.name, bank.bank_name))
            input("按任意键返回主页面")
        elif operation == "2":
            account = input("请输入要存钱账户: ")
            money = input("请输入要存钱金额: ")
            if bank.deposit(int(account), float(money)) == 1:
                print("存钱成功")
            else:
                print("存钱失败,用户不存在")
            input("按任意键返回主页面")
        elif operation == "3":
            account = input("请输入要取钱账户: ")
            money = input("请输入要取钱金额: ")
            res = bank.withdraw(int(account), float(money))
            if res == 1:
                print("取钱成功")
            elif res == 2:
                print("取钱失败,余额不足")
            else:
                print("取钱失败,用户不存在")
            input("按任意键返回主页面")
        elif operation == "4":
            old_account = input("请输入转账账户: ")
            new_account = input("请输入目标账户: ")
            money = input("请输入转账金额: ")
            res = bank.transfer(int(old_account), int(new_account), float(money))
            if res == 1:
                print("转账成功")
            elif res == 2:
                print("转账失败,余额不足")
            elif res == 3:
                print("转账失败,账户不存在")
            else:
                print("转账失败,转账账户和目标账户相同")
            input("按任意键返回主页面")
        elif operation == "5":
            print("查询功能")
            query_type = input("请输入查询类型: 1.查询全部  2.根据账户ID查询\n")
            if query_type == "1":
                if len(bank.user_dict) == 0:
                    print("-" * 20)
                    print("无用户信息")
                    print("-" * 20)
                    input("按任意键返回主页面")
                    continue
                print("查询全部账户信息如下: ")
                for _user in bank.user_dict.values():
                    print("-" * 20)
                    print(f"账户ID: {_user.account}")
                    print(f"姓名: {_user.name}")
                    print(f"密码: {_user.password}")
                    print(f"地址: {_user.address}")
                    print(f"余额: {_user.money}")
                    print(f"开户银行: {_user.bank}")
                print("-" * 20)
                input("按任意键返回主页面")
            elif query_type == "2":
                account = input("请输入要查询账户: ")
                _account = bank.query(int(account))
                if _account:
                    print("查询成功,用户信息如下: ")
                    print(f"账户ID: {_account.account}")
                    print(f"姓名: {_account.name}")
                    print(f"密码: {_account.password}")
                    print(f"地址: {_account.address}")
                    print(f"余额: {_account.money}")
                    print(f"开户银行: {_account.bank}")
                    input("按任意键返回主页面")
                else:
                    print("查询失败,用户不存在")
            else:
                print("输入有误,请重新输入")
        elif operation == "6":
            print("欢迎下次再来")
            exit()
        else:
            input("输入有误,按任意键返回")